<template>
    <div class="helper">
        <!-- {{unFinishedTodoLength}}调用计算属性 -->
        <span class="left">剩余{{unFinishedTodoLength}}未完成</span>
        <span class="tabs">
            <!-- 唯一的key，如果下一次循环key相同会复用 -->
            <span
                v-for="state in states"
                :key="state"
                :class="[state, filter === state ? 'actived' : '']"
                @click="toggleFilter(state)"
            >
                {{state}}
            </span>
        </span>
        <span
            class="clear"
            @click="clearAllCompleted"
        >
            清除已完成
        </span>
    </div>
</template>
<script>
    export default {
        props: {
            // 当前要选中的tab
            filter: {
                type: String,
                required: true
            },
            todos: {
                type: Array,
                required: true
            }
        },
        data () {
            return {
                states: ['all', 'active', 'completed']
            }
        },
        computed: {
            unFinishedTodoLength () {
                return this.todos.filter(todo => !todo.completed).length
            }
        },
        methods: {
            /**
             * 清除所有
             */
            clearAllCompleted () {
                this.$emit('clearAllCompleted')
            },
            /**
             * 修改状态
             */
            toggleFilter (state) {
                // 把tab的状态传递给父主键
                this.$emit('toggle', state)
            }
        }
    }
</script>
<style lang="stylus" scoped type="text/stylus">
    .helper {
        font-weight 100
        display flex
        justify-content space-between
        padding 5px 0
        line-height 30px
        background-color #ffffff
        font-size 14px
        font-smoothing: antialiased;
    }
    .left, .clear, .tabs {
        padding 0 10px
        box-sizing border-box
    }
    .left, .clear {
        width 150px
    }
    .left {
        text-align left
    }
    .clear {
        text-align: right
        cursor pointer
    }
    .tabs {
        width 200px
        display flex
        justify-content space-around
        * {
            display inline-block
            padding 0 10px
            cursor pointer
            border 1px solid rgba(175, 47, 47, 0)
            &.actived {
                border-color rgba(175, 47, 47, 0.4)
                border-radius 5px
            }
        }
    }
</style>